﻿$('#Main').live('pageinit', function (event) {

    $("#LoginButton").click(function () {

        var url = $(this).attr("data-url");
        var jsonData = "{ 'username' : '" + $('#username').val() + "', 'password' : '" + $('#password').val() + "', 'returnUrl' : '" + $('#returnUrl').val() + "'}";
        $.ajax({
            type: "POST",
            url: url,
            dataType: "json",
            contentType: "application/json; charset=utf-8",
            data: jsonData,
            processData: false
        })
        .fail(function (xhr) {
            displayMessage(xhr.responseText);
        })
        .done(function (json) {
            if (json.message != "")
                displayMessage("Login failed");
            else
                location.href = $("#returnUrl").val();
        });

        return false;
    });
});

$('#Module').live('pageinit', function (event) {

    // The page URL contains an "x-route" parameter for calling
    // the Content method of the controller. The "x-" prefix ensures
    // that the request is ignored by the page renderer when the
    // page is displayed. Here, we strip away the prefix and call
    // the same URL. This time, the page renderer will call the
    // controller method since it finds a properly formatted
    // route request (i.e. route=/{Method}/{Arg}

    var url = location.href.replace("x-", "");
    url = url.replace("#Module", "");
    var $page = $(this);

    // Get the content area element for the page.
    $content = $page.children(":jqmData(role=content)");

    $.ajax({
        type: "GET",
        url: url
    })
        .fail(function (xhr) {
            displayMessage(xhr.responseText);
        })
        .done(function (content) {
            $content.html(content);
        });

});
